<template>
	<view class="content">
		<!-- <view class="content_top"></view> -->
		<!-- <view class="content_space"></view> -->
		<!--收货地址-->
		<view class="address_view">
			<view class="line"></view>
			<view class="address">
				收货地址
			</view>
			<!--三角形-->
			<view class="sanjiao"></view>
			<view class="address_detail">
				<view class="address_name">{{useraddress}}</view>
				<view class="address_user">
					<view class="user">
						<view class="user_name">
							联系人：{{username}}
						</view>
						<view class="user_phone">
							联系方式：{{userphone}}
						</view>
					</view>
					<view class="update_address">
						<button class="btn" @click="chooseAddress">点击编辑</button>
					</view>
				</view>
			</view>
		</view>
		<!--商品列表-->
		<view class="shops">
			<block v-for="(shop_list, index_shop) in shopList" :key='index_shop'>
				<view class="shop_list">
					<view class="list_left">
						<!--选中按钮-->
						<view class="choose" @click="changeIcon(index_shop)">
							<view class="choose_icon"></view>
							<view :class="shop_list.chooseIcon == '1' ? '' : 'choose_selecticon'">
								<view :class="shop_list.chooseIcon == '1' ? '' : 'selecticon_chlid'" style="height: 20upx; width: 20upx;"></view>
							</view>
						</view>
						<view class="shop_name">{{shop_list.goods_name}}</view>
					</view>
					<view class="list_right">
						<view class="shop_price">
							<view style="font-size: 20upx; color: #EC0D0D;">￥</view>
							<view class="num">{{shop_list.sell_price}}</view>
						</view>
						<view class="count">
							<view class="sub" @click="subnum(index_shop)">－</view>
							<input class="shop_num" maxlength="2" type="number" :value="shop_list.buy_number" disabled="true" />
							<view class="add" @click="addnum(index_shop)">＋</view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<!--提交订单-->
		<view class="sub_view">
			<view class="amout">
				<view class="text">合计：</view>
				<view class="price">￥ {{total}}</view>
			</view>
			<view class="sub_order">
				<button class="btn" @click="toPay">提交订单</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Request
	} from "@/utils/request.js";
	var app = getApp();
	export default {
		data() {
			return {
				sum: 0,
				i: 0,
				arr: [],
				shopList: [],
				shopList_id: [],
				orderList: {},
				openId: '',
				username: '请填写姓名',
				userphone: '请填写电话',
				useraddress: '请填写收货地址',
			}
		},
		onLoad(option) {
			if (typeof option.goodsinfo !== "undefined" && option.goodsinfo !== null)
			{
				//从立即购买按钮进入，获取当此商品列表
				this.GoodsInfo = JSON.parse(decodeURIComponent(option.goodsinfo));
				this.toPay_goods_id = this.GoodsInfo.id;
				this.showCartList();
			}
			else
			{
				this.showCartList();
			}
		},
		//计算函数
		computed: {
			total() {
				var sum = 0;
				this.shopList.forEach(shop_list => {
					sum += shop_list.buy_number * shop_list.goods_price;
				});
				//保留两位小数
				return sum.toFixed(2);
			}
		},
		methods: {
			//传index,获取到指定index的shopnum,做判断
			addnum: function(index_shop) {
				let goods_id = this.shopList[index_shop].goods_id;
				new Request('goods/Cart_Add_Goods/AddCartGoods').request(
					() => {
						return 'POST'
					},
					() => {
						return {
							goods_id: goods_id,
							openid: app.globalData.openid,
							DevelopVersion: app.globalData.DevelopVersion,
							project: app.globalData.project
						}
					},
					(res) => {
						this.showCartList();
					}
				);
			},
			//减购
			subnum: function(index_shop) {
				let goods_id = this.shopList[index_shop].goods_id;
				new Request('goods/Cart_Subduction/SubductionCart').request(
					() => {
						return 'POST'
					},
					() => {
						return {
							goods_id: goods_id,
							openid: app.globalData.openid,
							DevelopVersion: app.globalData.DevelopVersion,
							project: app.globalData.project
						}
					},
					(res) => {
						this.showCartList();
					}
				);
			},
			//获取用户购物车列表
			showCartList: function(){
				new Request('goods/Cart_List/GetCartList').request(
					() => {
						return 'POST'
					},
					() => {
						if(typeof this.toPay_goods_id !== "undefined" && this.toPay_goods_id !== null){
							return {
								openid: app.globalData.openid,
								DevelopVersion: app.globalData.DevelopVersion,
								project: app.globalData.project,
								goods_id: this.toPay_goods_id
							}
						}else{
							return {
								openid: app.globalData.openid,
								DevelopVersion: app.globalData.DevelopVersion,
								project: app.globalData.project,
							}
						}
					},
					(res) => {
						this.shopList = res.data.data;
						if(res.data.data == 0){
							uni.navigateBack({
							    delta: 2
							});
							return;
						}
					}
				);
			},
			//去支付接口
			toPay: function() {
				if (app.globalData.Logincode == -1) {
					new Request().showToast('用户未登录');
					return;
				} else if (this.username == '请填写姓名' && this.userphone == '请填写电话' && this.useraddress == '请填写收货地址') {
					new Request().showToast('请正确的填写您的信息');
					return;
				}
				if(typeof this.toPay_goods_id !== "undefined" && this.toPay_goods_id !== null){
					//创建单个商品
					this.CreateInstantGoods();
				}else{
					//创建所有购物车订单
					this.CreateAllCart();
				}
				return;
				new Request('Order_Info/PushOrderInfo').request(
					() => {
						return 'POST'
					},
					() => {
						return {
							openid: app.globalData.openId,
							shop_car: JSON.stringify(this.shopList_id),
							username: this.username,
							userphone: this.userphone,
							useraddress: this.useraddress,
							DevelopVersion: app.globalData.DevelopVersion,
							project: app.globalData.project
						}
					},
					(res) => {
						this.orderList = res.data.data
						console.log(res.data.data)
						console.log(this.orderList)
						uni.removeStorage({
							key: 'shopcar_key'
						});
						//清除缓存后，重新加载整个小程序
						uni.reLaunch({
							url: '/pages/shopcar/pay/pay?orderList=' + encodeURIComponent(JSON.stringify(this.orderList)),
							success: (res) => {
								console.log('成功')
							}
						})
					});
			},
			//创建所有购物车商品列表
			CreateAllCart: function(){
				new Request('goods/Create_Instant_Goods/CreateInstantGoods').request(
					() => {
						return 'POST'
					},
					() => {
						return {
							openid: app.globalData.openid,
							customer_name: this.username,
							customer_phone: this.userphone,
							customer_address: this.useraddress,
							DevelopVersion: app.globalData.DevelopVersion,
							project: app.globalData.project,
						}
					},
					(res) => {
						if(res.data.code == 1){
							let orderid = res.data.order_id;
							uni.navigateTo({
								url: '/pages/shop/detail/pay/pay?orderid=' + orderid,
							});
						}else{
							new Request().showToast(res.data.msg);
						}
					}
				);
			},
			//创建单个商品
			CreateInstantGoods: function(){
				new Request('goods/Create_Instant_Goods/CreateOneGoods').request(
					() => {
						return 'POST'
					},
					() => {
						return {
							openid: app.globalData.openid,
							customer_name: this.username,
							customer_phone: this.userphone,
							customer_address: this.useraddress,
							DevelopVersion: app.globalData.DevelopVersion,
							project: app.globalData.project,
							goods_id: this.toPay_goods_id,
						}
					},
					(res) => {
						if(res.data.code == 1){
							let orderid = res.data.order_id;
							uni.navigateTo({
								url: '/pages/shop/detail/pay/pay?orderid=' + orderid + '&goodsinfo=' + encodeURIComponent(JSON.stringify(this.GoodsInfo)),
							});
						}else{
							new Request().showToast(res.data.msg);
						}
					}
				);
			},
			//传index_shop,切换选中状态
			changeIcon: function(index_shop) {
				if (this.shopList[index_shop].chooseIcon == '0') {
					this.shopList[index_shop].chooseIcon = '1'
				} else if (this.shopList[index_shop].chooseIcon == '1') {
					this.shopList[index_shop].chooseIcon = '0'
				}
				//在data定义数组，根据index_shop,将指定的price,放在数组arr的指定位置
				if (this.shopList[index_shop].chooseIcon == '1') {
					// this.arr[index_shop] = this.shopList_id[index_shop].price
					// console.log(this.arr)
					console.log(this.total)
				}
				//同上 将'0'放在index_shop 指定位置。
				if (this.shopList[index_shop].chooseIcon == '0') {
					// this.arr[index_shop] = 0
					// console.log(this.arr)
					// console.log(this.sum)
					console.log(this.total)
				}
			},
			//获取用户收货地址
			chooseAddress: function() {
				wx.chooseAddress({
					success: (res) => {
						this.username = res.userName
						this.userphone = res.telNumber
						this.useraddress = res.provinceName + res.cityName + res.countyName + res.detailInfo
					}
				})
			}
		}
		
	}
</script>

<style scoped>
	.content {
		width: 100%;
		height: 100%;
		background-color: #f5f5f5;
		position: absolute;
	}

	/*
		收货地址
	*/
	.content_top {
		background-color: #D93025;
		height: 60upx;
		position: fixed;
		width: 750upx;
		z-index: 999;
	}

	.content_space {
		height: 60upx;
	}

	.address_view {
		height: 336upx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		box-shadow: 0upx 4upx 12upx 0upx rgba(0, 0, 0, 0.12);
	}

	.line {
		height: 10upx;
		width: 750upx;
		background-color: #D93025;
	}

	.address {
		height: 76upx;
		width: 710upx;
		background-color: #e6d48c;
		color: #917810;
		font-weight: bold;
		font-size: 28upx;
		line-height: 76upx;
		padding-left: 40upx;
		border-top-left-radius: 12upx;
		border-top-right-radius: 12upx;
		box-shadow: 0upx 4upx 12upx 0upx rgba(0, 0, 0, 0.12);
	}

	.sanjiao {
		height: 0upx;
		width: 0upx;
		border: 25upx solid;
		margin: 0 auto;
		border-color: #e6d48c transparent transparent;
	}

	.address_detail {
		height: 165upx;
		margin: 0 25upx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.address_name {
		width: 495upx;
		height: 40upx;
		font-size: 28upx;
		font-weight: bold;
	}

	.address_user {
		height: 100upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.user {
		height: 100upx;
		width: 350upx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		font-size: 28upx;
	}

	.update_address {
		width: 220upx;
		height: 80upx;
		font-weight: bolder;
	}

	.btn {
		width: 220upx;
		height: 80upx;
		background-color: #EC0D0D;
		border-radius: 80upx;
		color: #FFFFFF;
		font-size: 28upx;
		line-height: 80upx;
		font-weight: bolder;
	}

	/*
		商品列表
	*/
	.shops {
		height: auto;
		padding: 40upx 0upx;
	}

	.shop_list {
		height: 100upx;
		padding: 0upx 35upx;
		margin-bottom: 20upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #FFFFFF;
	}

	.list_left {
		width: 320upx;
		height: 50upx;
		display: flex;
		align-items: center;
	}

	.list_right {
		width: 330upx;
		height: 60upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.choose {
		width: 48upx;
		height: 48upx;
		margin-right: 20upx;
		position: relative;
	}

	/*
		选中按钮样式
	*/
	.choose_icon {
		width: 30upx;
		height: 30upx;
		position: absolute;
		top: 15%;
		left: 15%;
		border-radius: 50upx;
		border: 5upx solid #808080;
		animation: fadeIn;
		animation-duration: 350ms;
	}

	.choose_selecticon {
		width: 30upx;
		height: 30upx;
		margin: auto;
		border-radius: 50upx;
		border: 5upx solid #EC0D0D;
		position: absolute;
		left: 15%;
		top: 15%;
		animation: fadeIn;
		animation-duration: 350ms;
	}

	.selecticon_chlid {
		background-color: #EC0D0D;
		border-radius: 50upx;
		position: absolute;
		top: 15%;
		left: 15%;
	}

	.shop_name {
		width: 235upx;
		height: 40upx;
		font-size: 28upx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.shop_price {
		display: flex;
		align-items: center;
	}

	.num {
		margin-left: 10upx;
		font-size: 40upx;
		font-weight: bold;
		color: #EC0D0D;
	}

	.count {
		width: 172upx;
		height: 60upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.sub {
		width: 45upx;
		height: 45upx;
		font-weight: bold;
		line-height: 45upx;
		display: flex;
		text-align: center;
		font-size: 44upx;
	}

	.add {
		width: 45upx;
		height: 45upx;
		font-weight: bold;
		line-height: 45upx;
		display: flex;
		text-align: center;
		font-size: 44upx;
	}

	.shop_num {
		width: 70upx;
		height: 50upx;
		background-color: #F8F8F8;
		border-radius: 18upx;
		text-align: center;
		font-size: 26upx;
	}

	/*
		提交订单
	*/
	.sub_view {
		height: 120upx;
		width: 680upx;
		background-color: #FFFFFF;
		position: fixed;
		z-index: 999;
		bottom: 0upx;
		padding: 0upx 35upx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.amout {
		height: 40upx;
		width: 350upx;
		display: flex;
	}

	.text {
		font-size: 28upx;
		width: 100upx;
	}

	.price {
		font-size: 36upx;
		color: #EC0D0D;
		font-weight: bold;
		line-height: 36upx;
	}

	.sub_order {
		height: 80upx;
		width: 220upx;
	}
</style>
